/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package Marks;

import Admission.AdmissionBean;
import ExamType.ExamTypeBean;
import Subject.SubjectBean;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.Iterator;
import java.util.Vector;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author Mukesh
 */
public class GetStuList extends HttpServlet {

    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        try {
            /* TODO output your page here. You may use following sample code. */
            int subjectid = Integer.parseInt(request.getParameter("subid"));
            int courseid = Integer.parseInt(request.getParameter("courseid"));
            int sem = Integer.parseInt(request.getParameter("sem"));
            Vector<ExamType.ExamTypeBean> list1 = ExamType.Query.viewAll();
            Vector<Admission.AdmissionBean> list = Subject.Query.getStuList(courseid, sem);
            SubjectBean sub = Subject.Query.viewSpecific(subjectid);
            Iterator it = list.iterator();
            Connection con = DBConnection.Connection.getConnection();
            while (it.hasNext()) {
                int i = 1;
                AdmissionBean ab = (AdmissionBean) it.next();
                Student.StudentBean sb = ab.getStudentBean();
                out.println("<tr>");
                out.println("<td>" + sb.getStudentName() + "</td>");
                out.println("<td>" + ab.getRollNumber() + "</td>");
                float total = 0;
                for (int k = 0; k < list1.size(); k++) {
                    Statement st = con.createStatement();
                    ResultSet rs = st.executeQuery("select marks from marks where admission_id=" + ab.getAddmissionId() + " and subject_id=" + subjectid + " and exam_type_id=" + list1.get(k).getExamTypeId() + " and semester=" + sem);
                    int marks = 0;
                    if (rs.next()) {
                        marks = rs.getInt(1);
                    }
                    out.println("<td><input style='width:100px;' name='" + ab.getAddmissionId() + "-" + list1.get(k).getExamTypeId() + "' title='"+list1.get(k).getWeightage()+"' id='" + ab.getAddmissionId() + "-" + k + "' type='text' value='" + marks + "' class='number' onblur=\"cal('" + ab.getAddmissionId() + "-','" + list1.size() + "','" + ab.getAddmissionId()  + "','" + sub.getTotalMarks()+ "');\" placeholder='" + list1.get(k).getExamTypeName() + "'></td>");
                    
                    if (list1.get(k).getWeightage() != 0) {
                        total = total+(((float)marks / (float)sub.getTotalMarks()) * list1.get(k).getWeightage());
                    }
                }
                out.println("<td><input type='text' id='" + ab.getAddmissionId() + "' value='" + total + "' readonly style='width:100px;'></td>");
                out.println("</tr>");
            }

        } catch (Exception e) {
            out.println(e);
        } finally {
            out.close();
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

}
